package com.team.recruitment_management_system.util.result;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * 分页响应结构体
 *

 */
@Data
public class PageResult<T> implements Serializable {

    private Integer code;

    private Data<T> data;

    private String message;

    protected static <T> PageResult<T> build(Data<T> data) {
        PageResult<T> pageResult = new PageResult<>();
        if (data != null)
            pageResult.setData(data);
        return pageResult;
    }

    public static <T> PageResult<T> build(Data<T> body, ResultCodeEnum resultCodeEnum) {
        PageResult<T> pageResult = build(body);
        pageResult.setCode(resultCodeEnum.getCode());
        pageResult.setMessage(resultCodeEnum.getMessage());
        return pageResult;
    }

    public static <T> PageResult<T> build(Data<T> body, Integer code,String message) {
        PageResult<T> pageResult = build(body);
        pageResult.setCode(code);
        pageResult.setMessage(message);
        return pageResult;
    }


    public static <T> PageResult<T> success(IPage<T> page) {
        PageResult<T> pageResult = build(null,ResultCodeEnum.SUCCESS);

        Data<T> data = new Data<>();
        data.setList(page.getRecords());
        data.setTotal(page.getTotal());
        pageResult.setData(data);
        return pageResult;
    }

    @lombok.Data
    public static class Data<T> {

        private List<T> list;

        private long total;

    }

}
